package com.composite;

import com.composite.entity.Employee;

public class Test {

    private static final String PREFIX_SYMBOL = "|   ";
    private static final String CURRENT_SYMBOL = "|-- ";

    public static void main(String[] args) throws Exception {
        /*
            适用于分级树状菜单，TreeAndLeaf。以员工与下属为例，这两个实例都为相同的抽象对象
         */
        // CEO(Chief Executive Officer) 首席执行官
        Employee boss = new Employee("Jack","CEO", 30000);
        // CFO(Chief Financial Officer) 首席财务官
        Employee CFO = new Employee("James","CFO", 20000);
        // CTO(Chief Technology Officer) 首席技术官
        Employee CTO = new Employee("Leon","CTO", 26000);
        // COO(Chief Operated Officer) 首席运营官
        Employee COO = new Employee("Kate","COO", 20000);
        // Developmental Engineer(Affiliated to CTO)
        Employee DE_1 = new Employee("Barry","DE", 18000);
        Employee DE_2 = new Employee("Selina","DE", 18000);
        // System Engineer(Affiliated to CTO)
        Employee SE = new Employee("Paul","SE", 15000);
        // 实习生
        Employee trainee = new Employee("Nancy","Trainee", 8000);

        // 从属关系建设
        boss.add(CFO);
        boss.add(CTO);
        boss.add(COO);

        CTO.add(DE_1);
        CTO.add(DE_2);
        CTO.add(SE);

        DE_2.add(trainee);

        // 树状打印
        Test test = new Test();
        test.dfsPrintTree(1, boss);
    }

    public static String padding_n(int n) throws Exception {
        if ( n < 1 )
            throw new NumberFormatException("Please make sure the starting number with at least number one.");
        StringBuilder placeholder = new StringBuilder("");
        if ( n == 1 )
            return placeholder.append(CURRENT_SYMBOL).toString();
        for (int i = 0; i < n-1 ; i++)
            placeholder.append(PREFIX_SYMBOL);
        placeholder.append(CURRENT_SYMBOL);
        return placeholder.toString();
    }

    /**
     * @Author: zishu
     * @Description:
     * @Date: 2022/1/9 16:38
     * @param depth 当前树深度，首次调用必须为1，后续调用依次加一
     * @param employee 员工信息
     **/
    public void dfsPrintTree(int depth, Employee employee) throws Exception {
        if ( employee == null )
            return;
        System.out.println(padding_n(depth) + employee.toTreeShow());
        if ( employee.getSubordinates() != null && !employee.getSubordinates().isEmpty() )
            for ( Employee ee : employee.getSubordinates() )
                dfsPrintTree(depth + 1, ee);
    }
}
/*
|-- A
|   |-- A-1
|   |-- A-2
|-- B
|-- C
|   |-- C-1
|   |-- C-2
|   |   |-- C-2_1
|   |-- C-3
|-- D
 */